/**
 * Created by yangxianming on 2018/4/20.
 */
//单词表
var currentWordList = [];
var confusionList = [];//临时存放乱序单词
var tempList = [];
var wrongWordList = [];
var orgList = [];
var startWord = 0;

//
//  api接口
//
function apiGetWords(){
    var errButton = document.getElementById("error");
    errButton.disabled = false;
    //step修改一组的数量
    var step = 16;
    var from = Number(selectedText(document.getElementById("sele1"))) * step - (step);
    var to = Number(selectedText(document.getElementById("sele2"))) * step;
    //是否加载遗忘数据
    var forget = "";
    var name = document.getElementById("b3").getAttribute("name");
    if(name == "2"){
        forget = "2";
    }
    if(name == "1"){
        forget = "1";
    }
    var body = {"from":from,"to":to,"forget":forget};
    wrongWordList = [];

    var data = {
        "action":"getWords",
        "body":body
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    startWord = 0;
                    currentWordList = r["result"];
                    orgList = [];
                    orgList = orgList.concat(r["result"]);
                    confusion();
                    if(currentWordList.length != 0){
                        drawWord(currentWordList[0]);
                        document.getElementById("s1").setAttribute("class", "collapse");
                    }
                }
            }
        }
    };
    xmlHttp1.open("POST", "/english/getWords", true);
    xmlHttp1.send(JSON.stringify(data));
}

function recordMistake(){
    var strList = [];
    var updateList = [];
    for(var i = 0; i < wrongWordList.length; i++){
        strList.push(wrongWordList[i][0]);
        updateList.push(wrongWordList[i]);
    }
    //遗忘模式增加
    var b2 = document.getElementById("b3");
    if(b2.getAttribute("name") == "2"){
        for(var x = 0; x < currentWordList.length; x++){
            updateList.push(currentWordList[x]);
        }
    }

    alert("forget: " + strList.length);

    var data = {
        "action":"mistakeWords",
        "body":updateList
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "/english/recordMistake", true);
    xmlHttp1.send(JSON.stringify(data));
}

//页面绘制

//显示当前单词
function drawWord(data){
	var bdLink = "https://fanyi.baidu.com/?aldtype=16047#en/zh/";
    document.getElementById("chinese").style.visibility="hidden";
    var wordData = data[0];
    var chineseData = data[1];
    var word = document.getElementById("word");
    var chinese = document.getElementById("chinese");
	word.onclick = function(){window.open(bdLink+wordData);};
    word.innerText = "";
    word.innerText = wordData;
    chinese.innerText = chineseData;
}

//轮询单词
function drawNext(){
    var errButton = document.getElementById("error");
    errButton.disabled = false;
    startWord += 1;
    //没有直接退出
    if(0 == currentWordList.length){
        apiGetWords();
        //alert("没有单词加载");
        return;
    }

    //如果是 遗忘 模式。记住的词可以扣除遗忘次数
    var b2 = document.getElementById("b3");
    if(b2.getAttribute("name") == "2"){
        if(currentWordList[startWord-1][2] != 0){
            currentWordList[startWord-1][2] = Number(currentWordList[startWord-1][2]) - 1;
        }
    }

    //判断到尾部
    if(startWord >= currentWordList.length){
        recordMistake();
        if(wrongWordList.length != 0){
            alert("把记错的再背一边吧");
            tempList = currentWordList;
            currentWordList = wrongWordList;
            wrongWordList = [];
        }else{
            alert("结束了,可以再来一边");
            if(tempList.length != 0){
                currentWordList = orgList;
                wrongWordList = [];
            }
            confusion();
        }
        startWord = 0;
        drawWord(currentWordList[0]);
        console.log(currentWordList);
        console.log(wrongWordList);
        return;
    }

    drawWord(currentWordList[startWord]);
}
function drawWrongMark(){
    var errButton = document.getElementById("error");
    errButton.disabled = true;
    if(currentWordList.length == 0){
        return;
    }
    if((wrongWordList.length == 0) || (wrongWordList[wrongWordList.length-1][0] != currentWordList[startWord][0])){
        wrongWordList.push(currentWordList[startWord]);
    }
    currentWordList[startWord][2] = Number(currentWordList[startWord][2]) + 1;
}

function drawButton2(){
    var b2 = document.getElementById("b2");
    var name = b2.getAttribute("name");
    b2.innerText = "";
    if(name == "1"){
        b2.setAttribute("name", "2");
        b2.innerText = "混乱";
    }else{
        b2.setAttribute("name", "1");
        b2.innerText = "秩序";
    }
}

function drawButton3(){
    var b2 = document.getElementById("b3");
    var name = b2.getAttribute("name");
    b2.innerText = "";
    if(name == "1"){
        b2.setAttribute("name", "2");
        b2.innerText = "遗忘";
    }else{
        b2.setAttribute("name", "1");
        b2.innerText = "普通";
    }
}

//
//打开页面后 需要加载一些数据
function initializeEnglish(){
    //设置中文解释点击显示
    document.getElementById("chineseParent").onclick = function(){document.getElementById("chinese").style.visibility="visible";};
    //加载给词按钮
    document.getElementById("b1").onclick = function(){apiGetWords()};
    document.getElementById("b2").onclick = function(){drawButton2()};
    document.getElementById("b3").onclick = function(){drawButton3()};
    //加载下个一个和错误按钮
    document.getElementById("next").onclick = function(){drawNext()};
    document.getElementById("error").onclick = function(){drawWrongMark()};

    //加载 组 下拉数据 每天一共50组，
    var sele1 = document.getElementById("sele1");
    var sele2 = document.getElementById("sele2");
    for (var i = 1; i<=52; i++){
        sele1.options.add(new Option(i, i));
        sele2.options.add(new Option(i, i));
    }
}
//根据 秩序/混乱 按钮确定单词列表是否要乱序
function confusion(){
    var name = document.getElementById("b2").getAttribute("name");
    if(name == "1"){
        currentWordList = orgList;
    }
    if(name == "2"){
        var le = currentWordList.length;
        for(var s = 0; s < le; s++){
            var currentLength = currentWordList.length;
            var rIndex = Math.floor(Math.random()*currentLength);
            confusionList.push(currentWordList[rIndex]);
            currentWordList.splice(rIndex, 1);
        }
        currentWordList = confusionList;
        confusionList = [];
    }
}